package cn.liontalk.microserviceorder.service.impl;

import cn.liontalk.microserviceorder.entity.Item;
import cn.liontalk.microserviceorder.properties.OrderProperties;
import cn.liontalk.microserviceorder.service.ItemService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@Service
public class ItemServiceImpl implements ItemService {

    // Spring框架对RESTful方式的http请求做了封装，来简化操作
    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private OrderProperties orderProperties;


    @Autowired
    private DiscoveryClient discoveryClient;


    @Value("${myspcloud.item.url}")
    private String itemUrl;


    @Override
    public Item queryItemById(long id) {
        // return this.restTemplate.getForObject(orderProperties.getItemProperties().getUrl() + id, Item.class);
//        Item item = this.restTemplate.getForObject(itemUrl + id, Item.class);
//        log.info("订单系统调用商品服务,result:" + item);
//        return item;
        log.info("orderProperties=====>{}",orderProperties.getItem().getUrl());
        String serviceId = "app-item";
        List<ServiceInstance> list = this.discoveryClient.getInstances(serviceId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        ServiceInstance serviceInstance = list.get(0);
        log.info("访问的端口是:===={}", serviceInstance.getPort());
        // String url =  serviceInstance.getHost() + ":" + serviceInstance.getPort();
        Item item = this.restTemplate.getForObject(itemUrl + id, Item.class);
        System.out.println("===========HystrixCommand queryItemById-线程池名称：" + Thread.currentThread().getName() + "订单系统调用商品服务,item:" + item);
        return item;
    }


   // @HystrixCommand(fallbackMethod = "queryItemByIdFallbackMethod")
    @Override
    public Item queryOrderByIdx(long id) {
        String serviceId = "app-item";
        List<ServiceInstance> list = this.discoveryClient.getInstances(serviceId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        ServiceInstance serviceInstance = list.get(0);
        log.info("访问的端口是:===={}", serviceInstance.getPort());
        Item item = this.restTemplate.getForObject(itemUrl + id, Item.class);
        System.out.println("===========HystrixCommand queryItemById-线程池名称：" + Thread.currentThread().getName() + "订单系统调用商品服务,item:" + item);
        return item;
    }


    /**
     * 请求失败执行的方法
     * fallbackMethod的方法参数个数类型要和原方法一致
     *
     * @param id
     * @return
     */
    public Item queryItemByIdFallbackMethod(long id) {
        return new Item(id, "查询商品信息出错!", null, null, null);
    }
}
